<template>
	<view class="">
		<!--  -->
		<ben></ben>
		<!--  -->
		<view class="background_f pad20 min30 rad1 column">
			<view class="row padbottom20 mintop30 align_item_c border_bottom just_cont_spacebet">
				<view class="font600 size28" style="color: #2E3C2A;">
					店铺logo
				</view>
				<image :src="userinfo.stores_logo" class="rad2" style="width: 100rpx;height: 100rpx;" @click="onImage">
				</image>
			</view>
			<view class="row padbottom20 mintop30 align_item_c border_bottom">
				<view class="font600 size28" style="color: #2E3C2A;width: 80px;">
					店名
				</view>
				<u--input placeholder="请输入您的店名" inputAlign="right" placeholderStyle="font-size: 28rpx;" border="none"
					v-model="userinfo.stores_name"></u--input>
			</view>
			<view class="row padbottom20 mintop30 align_item_c border_bottom">
				<view class="font600 size28" style="color: #2E3C2A;width: 80px;">
					门店地址
				</view>
				<view class="flex1" @click="onAdd">
					<u--input placeholder="请输入门店地址" inputAlign="right" placeholderStyle="font-size: 28rpx;"
						border="none" v-model="userinfo.stores_address"></u--input>
				</view>
			</view>
			<view class="row padbottom20 mintop30 align_item_c border_bottom">
				<view class="font600 size28" style="color: #2E3C2A;width: 80px;">
					门店负责人姓名
				</view>
				<u--input placeholder="请输入门店负责人姓名" inputAlign="right" placeholderStyle="font-size: 28rpx;" border="none"
					v-model="userinfo.shop_name"></u--input>
			</view>
			<view class="row padbottom20 mintop30 align_item_c border_bottom">
				<view class="font600 size28" style="color: #2E3C2A;width: 80px;">
					门店负责人电话
				</view>
				<u--input placeholder="请输入门店负责人电话" maxlength="11" inputAlign="right"
					placeholderStyle="font-size: 28rpx;" border="none" v-model="userinfo.shop_phone"></u--input>
			</view>
			<view class="row padbottom20 mintop30 align_item_c border_bottom">
				<view class="font600 size28" style="color: #2E3C2A;width: 80px;">
					门店负责人身份证
				</view>
				<u--input placeholder="请输入门店负责人身份证" maxlength="18" inputAlign="right"
					placeholderStyle="font-size: 28rpx;" border="none" v-model="userinfo.shop_id_card"></u--input>
			</view>
			<view class="column mintop30">
				<view class="font600 size28 minbottom20" style="color: #2E3C2A;">
					修改原因
				</view>
				<u--textarea v-model="reason" placeholder="请输入修改原因"></u--textarea>
			</view>
		</view>
		<view class="min30 padtop40">
			<u-button text="提交修改申请" shape="circle" color="linear-gradient( 90deg, #FFCC48 0%, #FF984E 100%)"
				@click="getApply">
			</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user_id: '',
				userinfo: {},
				longitude: '',
				latitude: '',
				reason: '',
			}
		},
		onLoad() {
			this.user_id = uni.getStorageSync('user_id');
			//当前页面展示的时候他就执行该生命周期
			const token = uni.getStorageSync('token');
			if (token) {
				//如果存在token
				this.getUserinfo()
				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {

				uni.showToast({
					title: '您还未登录，正在前往登录',
					icon: 'none'
				})
				setTimeout(function() {
					uni.navigateTo({
						url: "/mypackage/user/login"
					})
				}, 1000)
			}
		},
		methods: {
			onAdd() {
				var _this = this
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						_this.userinfo.stores_address = res.address
						_this.latitude = res.latitude
						_this.longitude = res.longitude
					}
				});
			},
			async getApply() {
				var _this = this
				var strTemp = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
				var id =
					/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/;
				if (!id.test(_this.userinfo.shop_id_card)) return this.$com.toast('身份证号输入不正确');
				if (!strTemp.test(_this.userinfo.shop_phone)) return this.$com.toast('手机号输入不正确');
				if (!_this.reason) return this.$com.toast('请输入修改原因');
				let {
					code,
					message,
					data
				} = await this.$com.fetch('member/application_shop/' + this.user_id, {
					shop_name: _this.userinfo.shop_name,
					shop_phone: _this.userinfo.shop_phone,
					shop_id_card: _this.userinfo.shop_id_card,
					stores_name: _this.userinfo.stores_name,
					reason: _this.reason,
					stores_logo: _this.userinfo.stores_logo,
					shop_add: _this.userinfo.shop_add,
					stores_longitude: _this.longitude ? _this.longitude : _this.userinfo.stores_longitude,
					stores_dimension: _this.latitude ? _this.latitude : _this.userinfo.stores_dimension,
				});

				if (code == 200) {
					uni.showToast({
						icon: 'success',
						title: message
					})
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/store/store'
						});
					}, 1000)
				} else {
					uni.showToast({
						icon: 'none',
						title: message
					})
				}

			},
			async getUserinfo() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("member/user_info/" + this.user_id)
				if (code == 200) {
					this.userinfo = data
				} else {
					console.log(message);
				}
			},
			onImage() {
				var _this = this
				uni.chooseImage({
					count: 1, // 图片数量
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						console.log(res)
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: 'https://xqs197810.com/api/member/upload', //上传图片api
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test',
								'ba-user-token': uni.getStorageSync('token')
							},
							header: {
								// token: uni.getStorageSync('token')
								// 'content-type': 'multipart/form-data;boundary=nitamayoubing',
							},
							success: (res) => {
								uni.hideLoading()
								let group = JSON.parse(res.data)
								// console.log(group);
								_this.userinfo.stores_logo = group.data
								uni.showToast({
									title: "上传成功",
									icon: "success"
								})
							},
							fail(res) {
								console.log(res);
							}
						});
					}
				});
			},
		}
	}
</script>

<style scoped lang="less">
	.border_bottom {
		border-bottom: 2rpx solid #EDEDED;
	}

	.box {
		background: linear-gradient(180deg, #C9DBFF 0%, #FFFFFF 34%);
	}
</style>